import { fetch } from '../services/homeGoods';
import { update } from '../services/homeGoodsAdd';

export default {
  namespace: 'homeGoodsAdd',

  state: {
    data: {
      list: [],
      pagination: {},
    },
  },

  effects: {
    * fetch({ payload = {} }, { call, put }) {
      Object.assign(payload, { status: 0, isHome: 0 });

      const response = yield call(fetch, payload);

      // 只显示首页的商品

      const { errno, pagination = {}, data = [] } = response;

      if (!errno) {
        const results = {
          pagination: {
            total: pagination.count,
            pageSize: pagination.size,
            current: pagination.from,
          },
          list: data,
        }

        yield put({
          type: 'save',
          payload: results,
        });
      }
    },
    * update({ payload = {}, callback }, { call, put, select }) {
      const response = yield call(update, payload);
      if (callback) callback(response);
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
  },
};